<template>
  <div class="content">
    <div class="pc">
      <el-carousel height="950px">
        <el-carousel-item v-for="(item, index) in 3" :key="index">
          <img src="../assets/banner/1.jpg" v-if="index == 0" alt="">
          <img src="../assets/banner/2.png" v-if="index == 1" alt="">
          <img src="../assets/banner/3.jpg" v-if="index == 2" alt="">
        </el-carousel-item>
      </el-carousel>
    </div>
    <!-- 产品中心 -->
    <div class="chanpin-box banner-box">
      <div class="title">
        <h1>PRODUCTS</h1>
        <h2>产品系列</h2>
      </div>
      <div class="item-box pc">
        <div class="item" v-for="(item, index) in LeftList" :key="index" @click="gotoChanpinList(item.record_id)">{{
          item.title }}</div>
      </div>
    </div>
    <div class="chanpin-data  banner-box">
      <div class="item" v-for="(item, index) in LeftList" :key="index" @click="gotoChanpinList(item.record_id)">
        <div><img :src="base64Img(item.img)" alt=""></div>
        <h1>{{ item.title }}</h1>
      </div>
    </div>
  </div>
</template>
<script setup>
import { getLeftList, getNewsList, getWebXinxi } from '@/api/viewapi.js'
import { onMounted, ref, reactive } from 'vue'
import { base64Img } from '@/tools/tools.js'
import router from '@/router/index.js';

const LeftList = ref([])
const LeftList2 = ref([])
const newsList = ref([])
const firstData = ref({})
const webxinxi = ref({})

// 左侧列表
const __getLeftData = () => {
  getLeftList().then((res) => {
    LeftList.value = res.data.data
    console.log('左侧信息', res);
    dataReact()
  })
}

// 获取新闻中心
const _getNewsList = () => {
  getNewsList().then((res) => {
    console.log('新闻列表', res);
    newsList.value = res.data.newslist
    firstData.value = res.data.newslist[0]

  })
}

// 获取网站信息
const _getWebXinxi = () => {
  getWebXinxi().then((res) => {

    console.log('网站信息', res);
    webxinxi.value = res.data.data
  })
}

// 跳转详情
const gotoXiangqing = (checkid, newsid) => {
  router.push({
    path: '/newsmessage', query: {
      checkid: checkid, newsid
    }
  })
}
// 跳转详情
const gotoChanpinList = (id) => {
  router.push({ path: '/chanpinlist', query: { id: id } })
}
// 跳转
const gotoChanpinXiangqing = (id) => {
  router.push({ path: '/chanpincontent', query: { id: id } })
}


// 跳转新闻页面
const gotoNewsView = () => {
  router.push({ path: '/news' })
}

// 轮播图数据处理
const dataReact = () => {
  let data = LeftList.value
  let pushData = []
  let item = []
  for (let i = 0; i < data.length; i++) {

    item.push(data[i])

    if (item.length === 6) {
      pushData.push({ item })
      item = []
    }
  }

  // 如果最后剩下不足 3 个元素，仍然需要推入
  if (item.length > 0) {
    pushData.push({ item });
  }

  LeftList2.value = pushData
  console.log('最后结果', pushData);
}

// 获取新闻列表


onMounted(() => {
  __getLeftData()
  _getNewsList()
  _getWebXinxi()
});

</script>
<style lang="scss" scoped>
// 取消显示手机端 显示pc端
@media (min-width: 800px) {
  .phone {
    display: none;
  }

  .chanpin-box {
    padding: 40px 0;
    display: flex;

    .title {
      margin-bottom: 10px;

      h1 {
        font-size: 24px;
        font-weight: 800;
      }

      h2 {}
    }

    .item-box {
      width: 100%;
      display: flex;
      justify-content: space-around;

      .item {
        line-height: 50px;
        cursor: pointer;
      }
    }
  }

  .chanpin-data {
    border-top: 1px solid gainsboro;
    padding-top: 40px;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
    padding: 10px;

    img {
      width: 100%;
    }

    .item {
      background-color: #ebf3f7;
      padding: 10px;
      cursor: pointer;

      div {
        padding: 10px;
      }

      h1 {
        text-align: center;
      }
    }
  }
}

// 取消显示pc端 显示手机端
@media screen and (max-width: 799px) {
  .pc {
    display: none;
  }

  .chanpin-box {
    padding: 20px;

    h1 {
      font-size: 24px;
      font-weight: 800;
    }
  }

  .chanpin-data {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    padding: 10px;
    .item {
      border-bottom: 1px solid gainsboro;
      padding: 10px;
    cursor: pointer;

      img {
        width: 100%;
      }

      h1 {
        text-align: center;
      }
    }
  }
}
</style>